package com.gavindon.android.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gavindon.android.MainActivity;
import com.gavindon.android.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * description:
 * Created by liNan on  2022-3-30 10:50
 */
public class LoadDialog extends Dialog {
    private TextView tvContent;

    public LoadDialog(@NonNull Context context) {
        super(context, R.style.payDialog);
        setContentView(R.layout.pay_result_dialog);
        setCanceledOnTouchOutside(false);
        tvContent = findViewById(R.id.tvDialogContent);

        rxjavaInterval();

    }

    public void showloading(String content) {
        tvContent.setText("支付成功3秒后跳转观看");
        super.show();
    }

    private Disposable subscribe;

    private void rxjavaInterval() {
        final Long time = 3L;
        subscribe = Observable.interval(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (aLong < time && !subscribe.isDisposed()) {
                            tvContent.setText("支付成功" + (time - aLong - 1) + "秒后跳转观看...");
                        } else {
                          /*  Intent intent = new Intent(WelcomActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();*/
                        }
                    }
                });
    }

    @Override
    public void dismiss() {
        super.dismiss();
        subscribe.dispose();
    }
}
